package org.example;

import org.example.bean.*;
import org.example.draw.MapDrawing;
import org.example.thread.ThreadPoolManager;
import org.example.thread.ThreadTask;
import org.example.util.RandomUtil;
import org.example.util.ThetaStarUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {


    public static void main(String[] args) throws InterruptedException {
        GameMap gameMap = new GameMap(1200, 1200);
        //初始化疆域
        MapMgr.getInstance().initMapGridTerritory(gameMap);
        //初始化静态阻挡
        MapMgr.getInstance().initMapGridBlockArray(gameMap);
        //预计算关卡之间的路径
        gameMap.initPassPath();
        // 创建地图窗口
        MapDrawing drawing = new MapDrawing(gameMap.getGrids(), "地图路径可视化");
        drawing.setVisible(true);
        for (int i = 0; i < 50; i++) {
            ThreadPoolManager.getInstance().addFindPathTask(new ThreadTask("FindPathThreadTask") {
                @Override
                protected void action() {
                    int x = RandomUtil.getRandom(1, 1200);
                    int y = RandomUtil.getRandom(1, 1200);
                    int x1 = RandomUtil.getRandom(1, 1200);
                    int y1 = RandomUtil.getRandom(1, 1200);
                    Grid girdByCoords1 = gameMap.getGrids()[x][y];
                    Grid girdByCoords2 = gameMap.getGrids()[x1][y1];
                    if (girdByCoords1.isStopBlock() || girdByCoords2.isStopBlock() || !girdByCoords1.isOpenTerritory() || !girdByCoords2.isOpenTerritory()) {
                        return;
                    }
                    JsonGamePos start = new JsonGamePos(x, y);
                    JsonGamePos end = new JsonGamePos(x1, y1);
                    List<Long> passIds = new ArrayList<>();
                    List<JsonGamePos> hashpath = ThetaStarUtil.findPath(start, end, gameMap, passIds, false);
                    //画图
                    if (!hashpath.isEmpty()) {
                        List<Cord> list = new ArrayList<>(hashpath.size());
                        for (JsonGamePos node : hashpath) {
                            list.add(new Cord((int) node.getX(), (int) node.getZ()));
                        }
                        drawing.addPath(list);
                    }
                }
            });
        }

    }

}